var path;
var tipDiv;
var keywordText;
var oldSelectedItem;
var prefixLineCount = 0;

function getData() {
	var result = "";
	keywordText = document.getElementById("keyword");
	var searchType = document.getElementById("searcher");
	var typeValue = searchType.options[searchType.selectedIndex].value.replace(/(^\s*)|(\s*$)/g, "");
	var keyword = keywordText.value.replace(/(^\s*)|(\s*$)/g, "");
	if (!keyword || !typeValue) {
		hiddenTip();
		return;
	}
	typeValue = searcherToType(typeValue);
	if(typeValue == null){
		hiddenTip();
		return;
	}
	var url = path + "/prefix?keyword=" + keywordText.value + "&type=" + typeValue;
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (req) {
		req.open("GET", encodeURI(url), false);
		req.onreadystatechange = function () {
			if (req.readyState == 4 && req.status == 200) {
				result = req.responseText;
			}
		};
		req.send();
	}
	return result;
}
function initPrefixTips(reqHead) {
	document.body.style.margin=0;
	keywordText = document.getElementById("keyword");
	keywordText.setAttribute("autocomplete","off");
	keywordText.setAttribute("disableautocomplete");
	path = reqHead;
	if (window.attachEvent) {
		keywordText.attachEvent("onpropertychange", showTip);
		keywordText.attachEvent("onblur", hiddenTip);
		keywordText.attachEvent("onclick", showTip);
		keywordText.attachEvent("onkeydown", keyEvent);
	} else {
		keywordText.addEventListener("input", showTip, false);
		keywordText.addEventListener("blur", hiddenTip, false);
		keywordText.addEventListener("click", showTip);
		keywordText.addEventListener("keydown", keyEvent);
	}
}
function keyEvent(){
	switch (event.keyCode) {
		case 13:
			if(oldSelectedItem){
				keywordText.value = oldSelectedItem.innerHTML;
				tipDiv.style.display = "none";
			}
		    break;
		case 38:
			moveItem(0);
		    break;
		case 40:
			moveItem(1);
		    break;
		case 27:
			oldSelectedItem = null;
			tipDiv.style.display = "none";
		    break;
		default:
			return;
    }
}
function moveItem(type){
	var selectedIdx=0;
	var newSelectedItem = null;
	if(oldSelectedItem){
		if(prefixLineCount==1){
			newSelectedItem=oldSelectedItem;
		}
		selectedIdx = oldSelectedItem.id.split("_")[1];
		if(type==0){
			if(parseInt(selectedIdx) == 1){
				newSelectedItem = document.getElementById("prefixLine_" + prefixLineCount);
			}else {
				newSelectedItem = document.getElementById("prefixLine_"+(parseInt(selectedIdx) - 1));
			}
		}else if(type == 1){
			if(parseInt(selectedIdx) == prefixLineCount){
				newSelectedItem = document.getElementById("prefixLine_1");
			}else {
				newSelectedItem = document.getElementById("prefixLine_"+(parseInt(selectedIdx) + 1));
			}
		}
	}else{
		if(type==0){
			newSelectedItem = document.getElementById("prefixLine_" + prefixLineCount);
		}else if(type == 1){
			newSelectedItem = document.getElementById("prefixLine_1");
		}
	}
	changeSelectedItem(newSelectedItem);
}
function changeSelectedItem(newSelectedItem){
	newSelectedItem.style.backgroundColor = "#000000";
	newSelectedItem.style.color = "#FFFFFF";
	if(oldSelectedItem && oldSelectedItem != newSelectedItem){
		oldSelectedItem.style.backgroundColor = "#FFFFFF";
		oldSelectedItem.style.color = "#000000";
	}
	oldSelectedItem = newSelectedItem;
}
function showTip() {
	oldSelectedItem = null;
	var result = getData();
	if (!result) {
		hiddenTip();
		return;
	}
	if (tipDiv == null) {
		tipDiv = document.createElement("div");
		tipDiv.id = "tipDiv";
		document.forms[0].appendChild(tipDiv);
	}
	tipDiv.innerHTML = result;
	tipDiv.style.top = (keywordText.offsetTop + keywordText.clientHeight + 3) + "px";
	tipDiv.style.left = keywordText.offsetLeft + "px";
	tipDiv.style.width = (keywordText.clientWidth + 2) + "px";
	tipDiv.style.display = "";
	tipDiv.style.position = "absolute";
	tipDiv.style.border = "solid #A7BFFB 1px";
	tipDiv.style.zIndex = 10000;
	tipDiv.style.backgroundColor = "#FFFFFF";
	tipDiv.style.textAlign="left";
	var prefixLines = tipDiv.getElementsByTagName("DIV");
	prefixLineCount = prefixLines.length;
	for (var i = 0; i < prefixLineCount; i++) {
		addEvent(prefixLines[i]);
	}
}
function hiddenTip() {
	if (tipDiv != null) {
		setTimeout(function () {
			tipDiv.style.display = "none";
		}, 200);
	}
}
function searcherToType(searcher) {
	if(searcher=="singerFuzzy_PcSearcher"){
		return 2;
	}else if(searcher=="singerPrecise_PcSearcher"){
		return 2;
	}else if(searcher=="allFuzzy_PcSearcher"){
		return 0;
	}else if(searcher=="allPrecise_PcSearcher"){
		return 0;
	}else if(searcher=="songPrecise_PcSearcher"){
		return 1;
	}else if(searcher=="songFuzzy_PcSearcher"){
		return 1;
	}else if(searcher=="albumPrecise_PcSearcher"){
		return 7;
	}else if(searcher=="song_WebSearcher"){
		return 1;
	}else if(searcher=="singer_WebSearcher"){
		return 3;
	}else if(searcher=="albumFuzzy_WebPcSearcher"){
		return 7;
	}
	return null;
}
function addEvent(obj) {
	obj.onmouseover = function () {
		changeSelectedItem(obj);
	};
	obj.onmouseout = function () {
		changeSelectedItem(obj);
		obj.style.backgroundColor = "#FFFFFF";
		obj.style.color = "#000000";
		oldSelectedItem = null;
	};
	obj.onclick = function () {
		keywordText.value = obj.innerHTML;
		keywordText.focus();
	};
	obj.style.cursor = "hand";
	obj.style.color = "#000000";
	obj.style.lineHeight="20px";
}


